<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\Common\MobMsg as MobMsg;
use AuctionCore\CarDealer\Dealer;
use AuctionCore\AntNest\Sms;

/**
 * Description of GroupMessageController
 *
 * @author xmh
 */
class GroupMessageController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        //页面头部信息
        $head = C('HEAD_INDEX');
        $this->assign("web_title", $head['title']);
        $this->assign("web_keywords", $head['keywords']);
        $this->assign("web_description", $head['description']);
        $this->emp_id = $this->user_info['emp_id'];
        //模板赋值
        $this->assign("emp_id", $this->emp_id);
    }

    public function index()
    {
        $map = [
            'cd.is_agree_agreement'=>['eq','1'],//同意签约
            'cd.status'=>['eq',1] //账号已启用
        ];
        $list = M("CarDealer")->alias('cd')
            ->field("cd.dealer_id,cd.contact_person,du.uid")
            ->join(C('DB_PREFIX') . "dealer_user as du ON cd.dealer_id=du.dealer_id")
            ->where($map)
            ->select();
        $dealer = new Dealer();
        $list = $dealer->get_contact_mobile_byAntNest($list);
        $this->assign("dealer", $list);
        $this->display();
    }

    public function messageSend()
    {
        $dealer_list = I("post.dealer_mlist");
        $scene_name = substr(I("post.scene_name"), 0, 16);
        $car_list = I("post.car_list");
        if (empty($dealer_list)) {
            $this->helpJsonResult(400, "车商联系人不能为空！");
        }
        if (empty($scene_name)) {
            $this->helpJsonResult(400, "拍卖时间不能为空！");
        }
        if (empty($car_list)) {
            $this->helpJsonResult(400, "车型名称不能为空！");
        }
        $dealer_arr = explode(",", $dealer_list);
        $Sms = new Sms();
        $mobile_rules = "/^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/";
        $data = array();
        foreach ($dealer_arr as $k => $v) {
            if (!preg_match($mobile_rules, $v)) {
                $data[$k]['code'] = '400';
                $data[$k]['msg'] = '手机号码格式不正确';
            } else {
                $r = $Sms->notify(11, $v, ['scene_name' => $scene_name, 'carlist' => $car_list]);
                if ($r) {
                    $data[$k]['code'] = '200';
                    $data[$k]['msg'] = '发送成功';
                } else {
                    $data[$k]['code'] = '500';
                    $data[$k]['msg'] = '手机号：' . $v . '发送短信失败';
                }
            }
        }
        if (!empty($data)) {
            $msg['code'] = '200';
            $msg['msg'] = '发送短信成功';
        } else {
            $msg['code'] = '500';
            $msg['msg'] = '发送短信失败';
        }
        echo json_encode($msg);
    }

}
